<?php


namespace app\user\controller;

use app\common\model\Bank;
use think\Request;
use app\common\model\UserFinance as financeModel;
use app\common\lib\Util;
use app\common\controller\Base;
use app\common\validate\LoginOrRegister as validate;

class Finance extends Base
{
    //金融产品列表
    public function index(financeModel $financeModel)
    {
        $financeList = $financeModel->where(array('user_id' => $this->userId))->field('id,unit_name')
            ->order('id desc')->select();
        return Util::apiResponse(['finance_list' => $financeList], '', 200);
    }

    //金融产品详情
    public function detail(financeModel $financeModel, Request $request)
    {
        $id = $request->param('id');
        $financeInfo = $financeModel->field('id,unit_name,identify_num,register_address,register_phone,bank_id,bank_number')
            ->with(['bank' => function ($query) {
                $query->field('id,name');
            }])
            ->where(['user_id' => $this->userId, 'id' => $id])
            ->find();
        return Util::apiResponse($financeInfo, '', 200);
    }


    //金融产品的添加
    public function add(Request $request, financeModel $financeModel)
    {
        $unitName = $request->unit_name;
        $identifyNum = $request->identify_num;
        $registerAddress = $request->register_address;
        $registerPhone = $request->register_phone;
        $bankId = intval($request->bank_id);
        $bankNumber = $request->bank_number;
        if (empty($unitName) && $unitName != '0') {
            return Util::apiResponse([], '请输入单位名称', 400);
        }
        if (empty($identifyNum) && $identifyNum != '0') {
            return Util::apiResponse([], '请输入识别号', 400);
        }
        if (empty($registerAddress) && $registerAddress != '0') {
            return Util::apiResponse([], '请输入注册地址', 400);
        }
        $validate = new validate();
        if (!$validate->check(['phone_num' => $registerPhone])) {
            return Util::apiResponse([], $validate->getError(), 400);
        }
        $bankExist = Bank::where(['id' => $bankId])->find();
        if (!$bankExist) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if (empty($bankNumber) && $bankNumber != '0') {
            return Util::apiResponse([], '请输入银行账号', 400);
        }
        $financeData = [
            'user_id' => $this->userId,
            'unit_name' => $unitName,
            'identify_num' => $identifyNum,
            'register_address' => $registerAddress,
            'register_phone' => $registerPhone,
            'bank_id' => $bankId,
            'bank_number' => $bankNumber
        ];
        $ins = $financeModel->save($financeData);
        if (!$ins) {
            return Util::apiResponse([], '操作失败', 400);
        }
        return Util::apiResponse([], '操作成功', 200);
    }

    //删除
    public function delete(Request $request,financeModel $financeModel){
        $id = $request->param('id');
        $exist = $financeModel->where(['id'=>$id,'user_id'=>$this->userId])->find();
        if(!$exist){
            return Util::apiResponse([], '参数错误', 400);
        }
        $result  = $financeModel->destroy(['id'=>$id,'user_id'=>$this->userId]);
        if(!$result){
            return Util::apiResponse([], '操作失败', 400);
        }
        return Util::apiResponse([], '操作成功', 200);
    }

}